package org.example.Server.service.question;

import org.example.Server.context.question.QsUpdateContext;
import org.example.Server.mapper.question.QuestionMapper;
import org.example.Server.model.question.*;
import org.example.Server.utils.ResultAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class QuestionService {
    @Autowired
    QuestionMapper questionMapper;
    @Autowired
    QsUpdateContext qsUpdateContext;

    @Transactional(rollbackFor = Exception.class)
    public ResultAPI insertQS(Question question) {
        try {
            questionMapper.insertQs(question, question.getIs_store());
            questionMapper.insertOption(question.getOptions());
            questionMapper.insertQSandOption(question);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("SQL operation failed", e);
        }
        return ResultAPI.success(question);
    }

    public ResultAPI getQSList(Integer techId, Integer qs_type, Integer difficulty, Integer point_id) {
        List<Question> questions;
        questions = questionMapper.getQSListByTechid(techId, qs_type, difficulty, point_id);
        if (questions != null && questions.size() != 0) {
            this.SelectAns(questions);
            return ResultAPI.success(questions);
        }
        return ResultAPI.error("出错了");
    }

    @Transactional(rollbackFor = Exception.class)
    public ResultAPI insertQnA(Question question) {
        try {
            questionMapper.insertQs(question, question.getIs_store());
            questionMapper.insertQandA(question);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return ResultAPI.success(question);
    }


    public ResultAPI deleteQS(List<Integer> qsIds) {
        questionMapper.deleteQS(qsIds);
        return ResultAPI.success();
    }


    //功能函数
    private void SelectAns(List<Question> qs) {
        for (Question qss : qs) {
            if (!qss.getQs_type().equals("qna")) {
                List<Option> options = qss.getOptions();
                String ans = "";
                for (Option option : options) {
                    if (option.getIs_right()) {
                        ans += (char) (options.lastIndexOf(option) + 'A');
                    }
                }
                qss.setRight_ans(ans);
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public ResultAPI updateQS(QUpdate question) {
//        List<Option> update_options = question.getUpdate_options();
//        List<Option> delete_options = question.getDelete_options();
//        List<Option> insert_options = question.getOptions();
        try {
//            questionMapper.updateQs(question);
//            QsUpdateContext qsUpdateContext=new QsUpdateContext();
            qsUpdateContext.QsUpdate(question);
//            if (question.getQs_type().equals("qna")) {
////                简答题
//                questionMapper.updateQna(question);
//            } else if (question.getQs_type().equals("yesorno")) {
////                判断题
//                if (update_options != null && !update_options.isEmpty()) {
//                    questionMapper.updateOption(question.getUpdate_options());
//                    questionMapper.updateQSandOption(question);
//                }
//            } else {
////                单选和多选
//                if (update_options != null && !update_options.isEmpty()) {
//                    questionMapper.updateOption(update_options);
//                    questionMapper.updateQSandOption(question);
//                }
//                if (insert_options != null && !insert_options.isEmpty()) {
//                    questionMapper.insertOption(insert_options);
//                    questionMapper.insertQSandOption(question);
//                }
//                if (delete_options != null && !delete_options.isEmpty()) {
//                    questionMapper.deleteOption(delete_options);
//                }
//            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return ResultAPI.success();
    }

    public ResultAPI getQuestionById(Integer qsId) {
        Question question = questionMapper.getQuestionById(qsId);
        return ResultAPI.success(question);
    }

    public ResultAPI getStuQsList(Integer userId, Integer qsType, Integer difficulty, Integer pointId) {
        List<Question> questions = questionMapper.getStuQsList(userId, qsType, difficulty, pointId);
        return ResultAPI.success(questions);
    }

    /**
     * 学生移除星标试题
     *
     * @param ids
     * @return
     */
    public ResultAPI deleteStuQsById(List<Integer> ids) {
        questionMapper.deleteStuQsById(ids);
        return ResultAPI.success();
    }

    /**
     * 学生收藏试题
     *
     * @param qsStarDTO
     * @return
     */
    public ResultAPI addStuStarQs(QsStarDTO qsStarDTO) {
        QsStarDTO qsStarExist = questionMapper.isExistQsStar(qsStarDTO);
        if (qsStarExist != null) {
            return ResultAPI.error("学生已收藏！");
        }
        questionMapper.addStuStarQs(qsStarDTO);
        return ResultAPI.success(qsStarDTO);
    }
}
